package com.jt.assistant.ui

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.reflect.TypeToken
import com.jt.assistant.R
import com.jt.assistant.adapter.CartonCodeListAdapter
import com.jt.assistant.adapter.ScanAdapter
import com.jt.assistant.base.GlobalConfig
import com.jt.assistant.bean.ScanCarton
import com.jt.assistant.viewmodel.HomeViewModel
import com.tencent.mmkv.MMKV
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_carton_state.*
import java.util.ArrayList

/**
 * 箱码列表
 */
class CartonStateActivity : BaseActivity<HomeViewModel>() {
    private val mAdapter by lazy {
        CartonCodeListAdapter(
            ArrayList<ScanCarton>()
        )
    }
    private val mScanCartonList = ArrayList<ScanCarton>()
    private val mScanCartonListSort = ArrayList<ScanCarton>()

    override fun layoutId(): Int = R.layout.activity_carton_state

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_CartonCode) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {
        MMKV.defaultMMKV()
            .getString(GlobalConfig.SCAN_CARTON_LIST, "").let {
                mScanCartonList.addAll(
                    GsonUtils.fromJson<ArrayList<ScanCarton>>(
                        it, object : TypeToken<ArrayList<ScanCarton>>() {}.type
                    )
                )
            }

        mScanCartonList.forEach {
            if (it.hasScan) {
                mScanCartonListSort.add(it)
            } else {
                mScanCartonListSort.add(0, it)
            }
        }

        mAdapter.data.addAll(mScanCartonListSort)
        mAdapter.notifyDataSetChanged()
    }

    override fun initListener() {
    }

}